package com.woniuxy.tfss.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.tfss.entity.*;
import com.woniuxy.tfss.service.CommentService;
import com.woniuxy.tfss.service.OrderService;
import com.woniuxy.tfss.service.RiderService;
import com.woniuxy.tfss.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Wrapper;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private RiderService riderService;

    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 打开订单评价弹窗
     *
     * @return
     */
    @GetMapping("init")
    public String evaluate() {
        return "comment";
    }

    /**
     * 为订单添加评分
     *
     * @param orderId
     * @param score
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    public ResponseEntity addComment(Integer orderId, Integer score) {
        Comment comment;
        Order order = orderService.getById(orderId);//根据订单id查询到该订单
        if (commentService.getByOrderId(orderId) != null) {
            comment = commentService.getByOrderId(orderId);
            comment.setScore(score);
//            commentService.updateById(comment);
        } else {
            comment = new Comment();
            comment.setOrderId(order.getId());
            comment.setRiderId(order.getRiderId());
            comment.setScore(score);
            comment.setStatus(Comment.NORMAL);
            comment.setContent("");
        }
        //更改或提交对当前订单的评分
        commentService.saveOrUpdate(comment);
        //更改订单状态为已评价
        order.setStatus(Order.REMARKED);
        orderService.updateById(order);
        //根据骑手id查询对该骑手的所有评价
        List<Comment> comments = commentService.getByRiderId(order.getRiderId());
//遍历得到骑手总评分


        Iterator<Comment> iter = comments.iterator();
        BigDecimal total = new BigDecimal("0.0");
        while (iter.hasNext()) {
            Comment c = (Comment) iter.next();
            total = total.add(new BigDecimal(c.getScore()));
        }
        //查询骑手评价数量
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("riderid", order.getRiderId());
        int num1 = commentService.count(wrapper);
        BigDecimal num2 = new BigDecimal(num1);
//得到骑手平均分
        BigDecimal average = new BigDecimal(String.valueOf(total.divide(num2, 2, BigDecimal.ROUND_HALF_UP)));
        System.out.println(average);
        //更新骑手评分
        Rider rider = riderService.getById(order.getRiderId());
        rider.setCredit(average.doubleValue());
        riderService.updateById(rider);
        webSocketServer.sendInfo("r"+order.getRiderId(),new Message<>(910,order));
        return ResponseEntity.SUCCESS;
    }
}
